package com.xxl.job.core.commons.annotation.impl;

import com.xxl.job.core.commons.annotation.SpaceCheck;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Description : 名字空格校验实现
 * @Author : wangqiangqiang
 * @Date : 11:35 2019/4/27
 * @Email : wangqiangqiang@cetcbigdata.com
 */
public class SpaceCheckImpl implements ConstraintValidator<SpaceCheck, String> {
    @Override
    public void initialize(SpaceCheck constraintAnnotation) {
    }

    @Override
    public boolean isValid(String name, ConstraintValidatorContext context) {
        // 检测字符串是否含有空格
        if (null != name && !"".equals(name)) {
            if (name.substring(0, 1).equals(" ") || name.substring(name.length() - 1).equals(" ") || name.trim().lastIndexOf(" ")!= -1) {
                return false;
            }
        }
        return true;
    }
}
